package com.clip.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.clip.action.util.SessionAttributeKey;
import com.clip.entity.User;
import com.clip.entity.UserQuestion;
import com.clip.service.UserQuestionService;
import com.clip.util.Struts2Utils;
import com.clip.util.ToolsUtils;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class UserQuestionAction extends ActionSupport{

	private static final long serialVersionUID = -3657170511123068146L;
	
	@Autowired
	private UserQuestionService userQuestionService;
	
	private byte[] buffer = new byte[1024*8];
	private SimpleDateFormat format_yMd = new SimpleDateFormat("yyyy-MM-dd");
	private String[] types = {"jpg","png","gif","jpeg","bmp"};
	private String title;
	private File uploadify;
	private String uploadifyFileName;
	private String context;

	@Override
	public String execute() throws Exception {
		return super.execute();
	}
	
	public void questSubmit(){
//		System.out.println(title);
//		System.out.println(uploadify);
//		System.out.println(uploadifyFileName);
//		System.out.println(context);
		String js = "";
		boolean uploadSave = false;
		title = title!=null&&title.trim().length()>0 ? title.trim() : null;
		context = context!=null&&context.trim().length()>0 ? context.trim() : null;
		HttpServletRequest request=ServletActionContext.getRequest();
		User user = (User)request.getSession().getAttribute(SessionAttributeKey.USER);
		if(user!=null&&title!=null&&context!=null){
			Date date = new Date();
			UserQuestion uq = new UserQuestion();
			uq.setTitle(ToolsUtils.htmlEnCode(title));
			uq.setContext(ToolsUtils.htmlEnCode(context));
			uq.setCreateDate(date.getTime());
			uq.setName(user.getName());
			uq.setUid(user.getId());
			if(uploadify!=null&&uploadifyFileName!=null){
				uq.setFilePath(this.format_yMd.format(date));
				String savedName = (Long.toHexString(date.getTime()) + uploadifyFileName.substring(uploadifyFileName.lastIndexOf("."))).toLowerCase();//图片保存名
				uq.setFileName(savedName);
				String uploadType =  uploadifyFileName.substring(uploadifyFileName.lastIndexOf(".")+1, uploadifyFileName.length()).toLowerCase();
				boolean b = false;
				for(String str : types){
					if(str.equals(uploadType)){
						b = true;
					}
				}
				if(b){
					String savePath = uq.getFileRoot()+uq.getFilePath();
					File file = new File(savePath);
					if(!file.exists()){
						file.mkdirs();
					}
					BufferedInputStream bis = null;
					BufferedOutputStream bos = null;
					try {
						bis = new BufferedInputStream(new FileInputStream(this.uploadify));
						bos = new BufferedOutputStream(new FileOutputStream(new File(uq.getFileRoot()+uq.getFilePath()+"\\"+uq.getFileName())));
						while(bis.read(buffer)>0){
							bos.write(buffer);
						}
						uploadSave = true;
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					} finally {
						try {if(bis!=null)bis.close();if(bos!=null)bos.close();} catch (IOException e) {e.printStackTrace();}
					}
				}else{
					js = "<script type='text/javascript'>parent.callBack(1,'文件类型错误');</script>";
				}
			}else{
				uploadSave = true;
			}
			if(uploadSave){
				userQuestionService.save(uq);
				js = "<script type='text/javascript'>parent.callBack(2,'提交成功！感谢您的支持');</script>";
			}
		}else{
			js = "<script type='text/javascript'>parent.callBack(0,'请输入标签或内容');</script>";
		}
		Struts2Utils.renderHtml(js);
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContext() {
		return context;
	}

	public void setContext(String context) {
		this.context = context;
	}

	public File getUploadify() {
		return uploadify;
	}

	public void setUploadify(File uploadify) {
		this.uploadify = uploadify;
	}

	public String getUploadifyFileName() {
		return uploadifyFileName;
	}

	public void setUploadifyFileName(String uploadifyFileName) {
		this.uploadifyFileName = uploadifyFileName;
	}
	
}